﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using WLong.BLL;

namespace WL_WinForm
{
    public partial class Article : DockContent
    {
        public Article()
        {
            InitializeComponent();
            Init();
            BindData();
            LoadHtmlEditor();
        }

        private void  Init()
        {
            DataTable dt = COMMON.ControlCommon.ConvertToComboList(
                BLL.wlBLL.BllComboList.GetArticleCategory(), "OtherCategoryName", "OtherCategoryID");
            COMMON.ControlCommon.ComboBinds(dt,cbArticleOtherCategoryID);
        }

        private void LoadHtmlEditor()
        {
            try
            {
                string editorPath = @"file:///" + System.Windows.Forms.Application.StartupPath + @"/Editor/editor.htm";
                editorPath = editorPath.Replace("\\", "/");
                webBrowser1.Navigate(editorPath);
            }
            catch
            {
                MessageBox.Show("加载编辑器失败！");
            }
        }

        private void BindData()
        {
            BLL.wlBLL.Acticle article = new BLL.wlBLL.Acticle();
            DataTable dt = article.GetAllArticle();
            gvArticle.DataSource = dt;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (cbArticleOtherCategoryID.SelectedValue.ToString() == "-999")
            {
                COMMON.ControlCommon.ShowMessageBoxError("请选择文章分类信息");
                return;
            }
            object content = this.webBrowser1.Document.InvokeScript("GetDetail");

            if (txtArticleID.Text != "")
            {
                WLong.Model.ArticleInfomation mArticle = new WLong.Model.ArticleInfomation();
                mArticle.ArticleID = new Guid(txtArticleID.Text);
                mArticle.ArticleTitle = txtArticleTitle.Text.Trim();
                mArticle.StartTime = deStartTime.Value;
                mArticle.EndTime = deStartTime.Value;
                mArticle.ArticleContent = content.ToString();
                mArticle.REC_ModifyBy = COMMON.Login.loginName;
                mArticle.REC_ModifyTime = DateTime.Now;

                WLong.Model.Rel_ArticleOtherCategory mArticleCategory = new WLong.Model.Rel_ArticleOtherCategory();
                mArticleCategory.ArticleOtherCategoryID = new Guid(txtArticleOtherCategoryID.Text);
                mArticleCategory.OtherCategoryID = new Guid(cbArticleOtherCategoryID.SelectedValue.ToString());
                mArticleCategory.ArticleInfomationID =new Guid (txtArticleID.Text);
                if (txtSort.Text !="")
                {
                    mArticleCategory.Sort = int.Parse(txtSort.Text);
                }
                mArticleCategory.REC_ModifyBy = COMMON.Login.loginName;
                mArticleCategory.REC_ModifyTime = DateTime.Now;

                BLL.wlBLL.Acticle bllActicle = new BLL.wlBLL.Acticle();
                int state = bllActicle.AddArticle(mArticle, mArticleCategory, false);
                if (state > 0)
                {
                    ClearControl();
                    BindData();
                    COMMON.ControlCommon.ShowMessageBoxOk("修改成功！");
                }
                else
                {
                    COMMON.ControlCommon.ShowMessageBoxOk("修改失败！");
                }
            }
            else
            {
                WLong.Model.ArticleInfomation mArticle = new WLong.Model.ArticleInfomation();
                mArticle.ArticleID = Guid.NewGuid();
                mArticle.ArticleTitle = txtArticleTitle.Text.Trim();
                mArticle.StartTime = deStartTime.Value;
                mArticle.EndTime = deStartTime.Value;
                mArticle.ArticleContent = content.ToString();
                mArticle.Disabled = 0;
                mArticle.REC_CreateBy = COMMON.Login.loginName;
                mArticle.REC_CreateTime = DateTime.Now;

                WLong.Model.Rel_ArticleOtherCategory mArticleCategory = new WLong.Model.Rel_ArticleOtherCategory();
                mArticleCategory.ArticleOtherCategoryID = Guid.NewGuid();
                mArticleCategory.OtherCategoryID = new Guid(cbArticleOtherCategoryID.SelectedValue.ToString());
                mArticleCategory.ArticleInfomationID = mArticle.ArticleID;
                if (txtSort.Text != "")
                {
                    mArticleCategory.Sort = int.Parse(txtSort.Text);
                }
                mArticleCategory.Disabled = 0;
                mArticleCategory.REC_CreateBy = COMMON.Login.loginName;
                mArticleCategory.REC_CreateTime = DateTime.Now;

                BLL.wlBLL.Acticle bllActicle = new BLL.wlBLL.Acticle();
                int state = bllActicle.AddArticle(mArticle, mArticleCategory, true);
                if (state > 0 )
                {
                    ClearControl();
                    BindData();
                    COMMON.ControlCommon.ShowMessageBoxOk("添加成功！");
                }
                else
                {
                    COMMON.ControlCommon.ShowMessageBoxOk("添加失败！");
                }
            }
        }

        public void SetDetailContent(string strContent)
        {
            webBrowser1.Document.InvokeScript("SetContent", new object[] { strContent });
        }

        private void ClearControl()
        {
            txtArticleID.Text = "";
            txtArticleTitle.Text = "";
            SetDetailContent("");
            deStartTime.Text = "";
            deEndTime.Text = "";
            txtArticleOtherCategoryID.Text = "";
            txtSort.Text = "";
            cbArticleOtherCategoryID.SelectedValue = "-999";
        }

        private void gvArticle_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            txtArticleID.Text = gvArticle.CurrentRow.Cells["ArticleID"].Value.ToString();
            txtArticleTitle.Text = gvArticle.CurrentRow.Cells["ArticleTitle"].Value.ToString();
            SetDetailContent(gvArticle.CurrentRow.Cells["ArticleContent"].Value.ToString());
            deStartTime.Text = gvArticle.CurrentRow.Cells["StartTime"].Value.ToString();
            deEndTime.Text = gvArticle.CurrentRow.Cells["EndTime"].Value.ToString();

            txtArticleOtherCategoryID.Text = gvArticle.CurrentRow.Cells["ArticleOtherCategoryID"].Value.ToString();
            txtSort.Text = gvArticle.CurrentRow.Cells["Sort"].Value.ToString();
            cbArticleOtherCategoryID.SelectedValue = gvArticle.CurrentRow.Cells["OtherCategoryID"].Value.ToString();
        }

        private void gvArticle_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                ArticleInfomation article = new ArticleInfomation();
                WLong.Model.ArticleInfomation mArticle = new WLong.Model.ArticleInfomation();
                mArticle.ArticleID = new Guid(gvArticle.CurrentRow.Cells["ArticleID"].Value.ToString());
                mArticle.ArticleTitle = gvArticle.CurrentRow.Cells["ArticleTitle"].Value.ToString();
                mArticle.StartTime = DateTime.Parse(gvArticle.CurrentRow.Cells["StartTime"].Value.ToString());
                mArticle.EndTime = DateTime.Parse(gvArticle.CurrentRow.Cells["EndTime"].Value.ToString());
                mArticle.ArticleContent = gvArticle.CurrentRow.Cells["ArticleContent"].Value.ToString();
                mArticle.Disabled = 1;
                mArticle.REC_CreateBy = gvArticle.CurrentRow.Cells["REC_CreateBy"].Value.ToString();
                mArticle.REC_CreateTime = DateTime.Parse(gvArticle.CurrentRow.Cells["REC_CreateTime"].Value.ToString());
                mArticle.REC_ModifyBy = COMMON.Login.loginName;
                mArticle.REC_ModifyTime = DateTime.Now;
                article.Update(mArticle);
                if (article.Update(mArticle))
                {
                    BindData();
                }
                else
                {
                    COMMON.ControlCommon.ShowMessageBoxError("删除失败！");
                }

            }
        }
    }
}
